package com.gzhu.knowledgeAdmin.controller;

import com.gzhu.knowledgeAdmin.common.config.GoFastDfsConfig;
import com.gzhu.knowledgeAdmin.common.constant.ExcelConstants;
import com.gzhu.knowledgeAdmin.common.result.ErrorCode;
import com.gzhu.knowledgeAdmin.common.result.R;
import com.gzhu.knowledgeAdmin.common.utils.GoFastDfsUtil;
import com.gzhu.knowledgeAdmin.common.valid.AddGroup;
import com.gzhu.knowledgeAdmin.common.valid.UpdateGroup;
import com.gzhu.knowledgeAdmin.common.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.gzhu.knowledgeAdmin.entity.Scene;
import com.gzhu.knowledgeAdmin.service.SceneService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Map;
import java.util.List;

/**
 * 场景表
 *
 * @author yangminggui
 * @email giaogiao@gmail.com
 * @date 2024-01-12 23:24:33
 */
@RestController
@RequestMapping("knowledgeAdmin/scene")
public class SceneController {
    @Autowired
    private SceneService sceneService;

    @Autowired
    private GoFastDfsUtil goFastDfsUtil;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageVo page = sceneService.queryPage(params);
        return R.ok(page);
    }
    /**
     * 所有场景
     */
    @GetMapping("/getAll")
    public R getAll(){
        List<Scene> sceneList = sceneService.getAll();
        return R.ok(sceneList);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		Scene scene = sceneService.getById(id);
        return R.ok(scene);
    }

    /**
     * 添加
     */
    @PostMapping("/save")
    public R save(@RequestBody @Validated(AddGroup.class) Scene scene){
		sceneService.saveScene(scene);
        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody @Validated(UpdateGroup.class) Scene scene){
            sceneService.updateById(scene);
        return R.ok();
    }


    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody List<Integer> ids){
        try {
            sceneService.removeByIds(ids);
        } catch (Exception e) {
            e.printStackTrace();
            // 明确提示前端错误信息
            return R.error(ErrorCode.USED_ERROR);
        }
        return R.ok();
    }

    /**
     * 场景导入模板下载
     *
     * @param response
     * @throws IOException
     */
    @GetMapping("/template")
    public void downloadTemplate(HttpServletResponse response) throws IOException, URISyntaxException {
        goFastDfsUtil.downloadFile(goFastDfsUtil.getServicePath()+'/'+ ExcelConstants.SCENE_TEMPLATE_URL,response);
    }


    /**
     * 导入场景
     *
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/_import")
    public R importKeyword(MultipartFile file) throws IOException {
        String msg = sceneService.importScene(file);
        return R.ok(msg);
    }

    /**
     * 导出场景
     *
     * @param response
     * @throws IOException
     */
    @GetMapping("/_export")
    public void exportScene(@RequestParam Map<String, Object> params, HttpServletResponse response) {


        sceneService.exportScene(params,response);

    }


}
